﻿<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <link href="/css/bootstrap.css" rel="stylesheet" />
    <link href="/css/bootstrap-theme.css" rel="stylesheet" />
    <link href="/css/admin.css" rel="stylesheet" />
    <script src="/js/jquery.js"></script>
    <script src="/js/ModuleLoader.js"></script>
    <script src="/js/bootstrap.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script src="/js/FastHttpApi.js"></script>
    <script src="/js/admin/AdminSetting.js"></script>
    <script src="/js/marked.js"></script>
    <script src="/js/lodash.js"></script>
    <title>BeetleX Blog Admin</title>
</head>
<body>

    <div class="navbar navbar-inverse navbar-fixed-left" slot="header">
    </div>
    <div class="page-title">
        <img src="/images/admin/nav.png" /> <h3>设置</h3>
    </div>
    <div class="container bs-docs-container">
        <div class="row" id="settingEditor">
            <form class="form-group-sm">
                <div class="form-group">
                    <label for="exampleInputEmail1">网站标题</label>
                    <input type="text" v-model:value="Data.Title" class="form-control">
                </div>

                <div v-if="Data.ESStatus" class="form-group has-success has-feedback">
                    <label for="exampleInputPassword1">ElasticSearch服务地址</label>
                    <input type="text" v-model:value="Data.ESHost" class="form-control" placeholder="http://localhost:9200">


                    <input type="text" id="txtWord" class="form-control" placeholder="输入分词内容" style="margin-bottom:4px;margin-top:4px;">
                    <button type="button" onclick="testES()" class="btn btn-default btn-sm">测试分词</button>
                    <button type="button" onclick="recreateIndex()" class="btn btn-default btn-sm">重建索引</button>


                </div>
                <div v-else class="form-group has-error has-feedback">
                    <label for="exampleInputPassword1">ElasticSearch服务地址</label>
                    <input type="text" v-model:value="Data.ESHost" class="form-control" placeholder="http://localhost:9200">
                    {{Data.ESError}}
                    <button type="button" onclick="recreateIndex()" class="btn btn-default btn-sm">重建索引</button>
                </div>


                <div class="form-group">
                    <label>关于(Markdown)</label>
                    <a href="javascript:void(0)" onclick="$('#txtAboutHtml').hide(); $('#txtAbout').show()">编辑</a>
                    <a href="javascript:void(0)" onclick="$('#txtAboutHtml').show(); $('#txtAbout').hide()">预览</a>
                    <textarea id="txtAbout" :value="Data.About" @input="update" class="form-control" rows="10"></textarea>
                    <div id="txtAboutHtml" class="form-control" v-html="compiledMarkdown" style="height:200px;overflow-y: auto; overflow-x:hidden; width:100%;display:none;"></div>
                </div>
                <div class="form-group">
                    <label for="exampleInputEmail1">站内图片域名:</label>
                    <input type="text" v-model:value="Data.ImgHost" class="form-control" placeholder="img1.ikende.com;img2.ikende.com">
                </div>
                <button type="button" onclick="save()" class="btn btn-default btn-sm">保存</button>


            </form>
            <hr />
            <form class="form-group-sm">

                <div class="form-group">
                    <label class="form-group">JSON Web Tokey Key</label>
                    <input type="text" :value="Data.KEY" class="form-control" readonly>
                </div>
                <button type="button" onclick="reCreateKey()" class="btn btn-default btn-sm">重新生成</button>
            </form>
            <hr />
            <form class="form-group-sm">
                <div class="form-group">
                    <label>腾讯云SecretId</label>
                    <input type="text" v-model:value="TCould.SecretId" class="form-control" placeholder="SecretId">
                </div>
                <div class="form-group">
                    <label>腾讯云SecretKey</label>
                    <input type="text" v-model:value="TCould.SecretKey" class="form-control" placeholder="SecretKey">
                </div>
                <div class="form-group">
                    <label>存储桶地址</label>
                    <input type="text" v-model:value="TCould.Host" class="form-control" placeholder="https://*.cos.ap-guangzhou.myqcloud.com">
                </div>
                <button type="button" onclick="saveTCould()" class="btn btn-default btn-sm">修改</button>
            </form>
            <hr />
            <form class="form-group-sm">
                <div class="form-group">
                    <label>新密码</label>
                    <input type="password" v-model:value="Password.NEW" class="form-control" placeholder="新密码">
                </div>
                <div class="form-group">
                    <label>确认密码</label>
                    <input type="password" v-model:value="Password.RNEW" class="form-control" placeholder="确认密码">
                </div>
                <button type="button" onclick="changePWD()" class="btn btn-default btn-sm">修改密码</button>
            </form>


        </div>
    </div>

    <div class="container" style="text-align:center;" slot="footer">

    </div>
    <script>
        var settingEditor;
        $(document).ready(function () {
            settingEditor = new Vue({
                el: '#settingEditor',
                data: {
                    Data: { Title: "", ESHost: "", About: "", ImgHost: "" },
                    Password: { NEW: "", RNEW: "" },
                    TCould: { SecretId: "", SecretKey: "", Host: "" }

                },
                computed: {
                    compiledMarkdown: function () {
                        return marked(this.Data.About, { sanitize: true })
                    }
                },
                methods: {
                    update: _.debounce(function (e) {
                        this.Data.About = e.target.value
                    }, 300)
                }
            });
            AdminSettingGetTCloudInfo().execute(function (r) {
                settingEditor.TCould = r.Data;
            });
            GetSetting();
        });

        function saveTCould() {
            if (confirm('是否要保存腾讯云配置信息?')) {
                AdminSettingSaveTCloudInfo(
                    settingEditor.TCould.SecretId,
                    settingEditor.TCould.SecretKey,
                    settingEditor.TCould.Host).execute(function (r) {
                        alert("设置已保存!");
                        GetSetting();
                    });
            }
        }

        function reCreateKey() {
            if (confirm('重新生成JSON Web Tokey Key，是否继续？')) {
                AdminSettingReCreateJWT().execute(function (r) {
                    alert("已经生成成功!");
                    GetSetting();
                });
            }
        }
        function recreateIndex() {
            if (confirm('是否要重建索，是否继续？')) {
                AdminSettingReCreateIndex().execute(function (r) {
                    alert("重建索引完成，请在文章管理里重新同步数据!");
                });
            }
        }
        function testES() {
            var word = $('#txtWord').val();
            if (!word) {
                alert('输入测试分语的内容');
                return;
            }
            AdminSettingESTest(word).execute(function (r) {
                alert(result.Data);
            });
        }
        function changePWD() {
            if (!settingEditor.Password.NEW || (settingEditor.Password.NEW != settingEditor.Password.RNEW)) {
                alert("新密码为空或不一致,请重新输入!")
                return;
            }
            AdminSettingChangePwd(settingEditor.Password.NEW,
                settingEditor.Password.RNEW).execute(function (r) {
                    alert("密码修改成功!");
                });
        }
        function save() {
            AdminSettingUpdateSetting(settingEditor.Data.Title,
                settingEditor.Data.ESHost, settingEditor.Data.About,
                settingEditor.Data.ImgHost).execute(function (r) {
                    alert("信息设置成功!")
                    GetSetting();
                });
        }
        function GetSetting() {
            AdminSettingGetSetting().execute(function (r) {
                settingEditor.Data = r.Data;
            });

        }
    </script>
</body>

</html>